import 'dart:async';
import 'dart:convert';
import 'package:get/get.dart';
import 'package:ichinesereader/models/book/RecordLocation.dart';
import '../service/recorder.dart';

class RecorderController extends GetxController with StateMixin<dynamic> {
  RecorderServiceProvider _recorderServiceProvider =
      new RecorderServiceProvider();
  RxString _bookRecordingLocation = ''.obs;
  RxString get bookRecordingLocation => this._bookRecordingLocation;

  RxBool _isRecording = false.obs;
  RxBool get isRecording => this._isRecording;

  RxBool _isPaused = false.obs;
  RxBool get isPaused => this._isPaused;

  RxBool _showSave = false.obs;
  RxBool get showSave => this._showSave;
  RxBool _showPlayer = false.obs;
  RxBool get showPlayer => this._showPlayer;

  RxInt _recordDuration = 0.obs;
  RxInt get recordDuration => this._recordDuration;

  RxString _path = ''.obs;
  RxString get path => this._path;
  RxString _assignmentId = ''.obs;
  RxString get assignmentId => this._assignmentId;

  RxString _bookId = ''.obs;
  RxString get bookId => this._bookId;

  List<int> _audioBytes = <int>[].obs;
  List<int> get audioBytes => this._audioBytes;

  Timer? _timer;

  //@override
  void onInit() {
    super.onInit();
    _timer?.cancel();
    _isRecording.value = false;
    _showSave.value = false;
    _path.value = '';
    _recordDuration.value = 0;
    _isPaused.value = false;
    _audioBytes.clear();
  }

  @override
  dispose() {
    super.dispose();
    _timer?.cancel();
    _isRecording.value = false;
    _showSave.value = false;
    _path.value = '';
    _recordDuration.value = 0;
    _isPaused.value = false;
    _audioBytes.clear();
  }

  // void setToDefault() {
  //   _timer?.cancel();
  //   _isRecording.value = false;
  //   _showSave.value = false;
  //   _showPlayer.value = false;
  //   _path.value = '';
  //   _recordDuration.value = 0;
  //   _isPaused.value = false;
  //   _bookRecordingLocation.value = '';
  //   _audioBytes.clear();
  // }

  void setRecordTime() {
    _timer?.cancel();
    _timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
      _recordDuration.value++;
    });
  }

  void startRecordState() {
    _isRecording.value = true;
    _showSave.value = false;
  }

  void setInitPlayer() {
    _showPlayer.value = true;
  }

  void stopRecordState(path) {
    _timer?.cancel();
    _isRecording.value = false;
    _showSave.value = true;
    _showPlayer.value = true;
    _path.value = path;
    _recordDuration.value = 0;
    _isPaused.value = false;
  }

  void closeRecordState() {
    _timer?.cancel();
    _isRecording.value = false;
    _showSave.value = false;
    _showPlayer.value = false;
    _bookRecordingLocation.value = '';
    _recordDuration.value = 0;
    _isPaused.value = false;
    _audioBytes.clear();
  }

  void pauseRecordState() {
    _timer?.cancel();
    _isPaused.value = true;
  }

  void resumeRecordState() {
    _isPaused.value = false;
  }

  void setBookId(bookId) async {
    _bookId.value = bookId;
  }

  void setAssigmentId(assignmentId) async {
    _assignmentId.value = assignmentId;
  }

// to do
  // void checkRecHistory(bookId, assignmentId) async {
  //   Future<String?> result = getRecording(bookId, assignmentId);
  // }

  void fetchBookRecordingFile(bookId, assignmentId) async {
    try {
      var response =
          await _recorderServiceProvider.getRecording(bookId, assignmentId);
      if (bookId != null) this._bookId.value = bookId;
      if (assignmentId != null) this._assignmentId.value = assignmentId;

      if (response.statusCode == 200) {
        var data = recordLocationFromJson(response.bodyString);
        String? fileId = data.location;
        if (fileId != null && fileId != '') {
          response =
              await _recorderServiceProvider.downloadFileLocation(fileId);
          _bookRecordingLocation.value =
              jsonDecode(response.bodyString)['location'];
          _showPlayer.value = true;
          change(_bookRecordingLocation, status: RxStatus.success());
          change(_showPlayer, status: RxStatus.success());
        } else {
          _showPlayer.value = false;
          this._bookRecordingLocation.value = '';
          change(_bookRecordingLocation, status: RxStatus.empty());
          change(_showPlayer, status: RxStatus.success());
        }
      }
    } catch (error) {
      print(error);
      this._showPlayer.value = false;
    }
  }
}
